package com.zxq.web.assist;

import com.zxq.web.controller.IOTProductController;
import com.zxq.web.dto.PlatformProjectDTO;
import com.zxq.web.enums.EProductState;
import com.zxq.web.iot.domain.IotProject;
import com.zxq.web.iot.service.impl.IotProjectServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

@Service
@Slf4j
public class ActivateServer {



    @Autowired
    IotProjectServiceImpl iotProjectDoService;

    @Autowired
    IOTProductController iotProductController;

    /**
     * 重启产品任务
     */
    @PostConstruct
    public void restartProduct() {

        IotProject queryCondition = new IotProject();
        queryCondition.setState(EProductState.run.getValue().longValue());
        List<IotProject> productDoList =  iotProjectDoService.selectIotProjectList(queryCondition);

        if(productDoList.size() == 0) {
            log.info("not restart product!");
            return;
        }
        productDoList.forEach(iotProductDo -> {
            PlatformProjectDTO productDTO = new PlatformProjectDTO();
            productDTO.setProjectId(iotProductDo.getId());
            productDTO.setOperatorId(iotProductDo.getCreator());
            iotProductController.startProductServer(productDTO);
        });

    }



}
